home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / devel / tcl / tclx7_31.z / tclx7_31 / tcldev / tclX7.3a-p1 / src / tclXmath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-25  |  5.2 KB  |  203 lines

  1. /*
  2.  * tclXmath.c --
  3.  *
  4.  * Mathematical Tcl commands.
  5.  *-----------------------------------------------------------------------------
  6.  * Copyright 1991-1993 Karl Lehenbauer and Mark Diekhans.
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies.  Karl Lehenbauer and
  11.  * Mark Diekhans make no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without express or
  13.  * implied warranty.
  14.  *-----------------------------------------------------------------------------
  15.  * $Id: tclXmath.c,v 3.2 1994/01/25 01:07:01 markd Exp $
  16.  *-----------------------------------------------------------------------------
  17.  */
  18.  
  19. #include "tclExtdInt.h"
  20.  
  21. /*
  22.  * Prototypes of random functions, since we may be using one out of osSupport.
  23.  * This breaks with GNU libc headers...really should check with autoconf.
  24.  */
  25.  
  26. #ifndef __GNU_LIBRARY__
  27. long random ();
  28. #endif
  29.  
  30. /*
  31.  * Prototypes of internal functions.
  32.  */
  33. static long 
  34. ReallyRandom _ANSI_ARGS_((long my_range));
  35.  
  36.  
  37. /*
  38.  *-----------------------------------------------------------------------------
  39.  *
  40.  * Tcl_MaxCmd --
  41.  *      Implements the TCL max command:
  42.  *        max num1 num2 ?..numN?
  43.  *
  44.  * Results:
  45.  *      Standard TCL results.
  46.  *
  47.  *-----------------------------------------------------------------------------
  48.  */
  49. int
  50. Tcl_MaxCmd (clientData, interp, argc, argv)
  51.     ClientData  clientData;
  52.     Tcl_Interp *interp;
  53.     int         argc;
  54.     char      **argv;
  55. {
  56.     double value, maxValue = -MAXDOUBLE;
  57.     int    idx,   maxIdx   =  1;
  58.  
  59.  
  60.     if (argc < 3) {
  61.         Tcl_AppendResult (interp, tclXWrongArgs, argv [0], 
  62.                           " num1 num2 ?..numN?", (char *) NULL);
  63.         return TCL_ERROR;
  64.     }
  65.  
  66.     for (idx = 1; idx < argc; idx++) {
  67.         if (Tcl_GetDouble (interp, argv [idx], &value) != TCL_OK)
  68.             return TCL_ERROR;
  69.         if (value > maxValue) {
  70.             maxValue = value;
  71.             maxIdx = idx;
  72.         }
  73.     }
  74.     strcpy (interp->result, argv [maxIdx]);
  75.     return TCL_OK;
  76. }
  77.  
  78. /*
  79.  *-----------------------------------------------------------------------------
  80.  *
  81.  * Tcl_MinCmd --
  82.  *     Implements the TCL min command:
  83.  *         min num1 num2 ?..numN?
  84.  *
  85.  * Results:
  86.  *      Standard TCL results.
  87.  *
  88.  *-----------------------------------------------------------------------------
  89.  */
  90. int
  91. Tcl_MinCmd (clientData, interp, argc, argv)
  92.     ClientData  clientData;
  93.     Tcl_Interp *interp;
  94.     int     argc;
  95.     char      **argv;
  96. {
  97.     double value, minValue = MAXDOUBLE;
  98.     int    idx,   minIdx   = 1;
  99.  
  100.     if (argc < 3) {
  101.         Tcl_AppendResult (interp, tclXWrongArgs, argv [0], 
  102.                           " num1 num2 ?..numN?", (char *) NULL);
  103.         return TCL_ERROR;
  104.     }
  105.  
  106.     for (idx = 1; idx < argc; idx++) {
  107.         if (Tcl_GetDouble (interp, argv [idx], &value) != TCL_OK)
  108.             return TCL_ERROR;
  109.         if (value < minValue) {
  110.             minValue = value;
  111.             minIdx = idx;
  112.             }
  113.         }
  114.     strcpy (interp->result, argv [minIdx]);
  115.     return TCL_OK;
  116. }
  117.  
  118. /*
  119.  *-----------------------------------------------------------------------------
  120.  *
  121.  * ReallyRandom --
  122.  *     Insure a good random return for a range, unlike an arbitrary
  123.  *     random() % n, thanks to Ken Arnold, Unix Review, October 1987.
  124.  *
  125.  *-----------------------------------------------------------------------------
  126.  */
  127. #define RANDOM_RANGE 0x7fffffffL
  128.  
  129. static long 
  130. ReallyRandom (myRange)
  131.     long myRange;
  132. {
  133.     long maxMultiple, rnum;
  134.  
  135.     maxMultiple = RANDOM_RANGE / myRange;
  136.     maxMultiple *= myRange;
  137.     while ((rnum = random ()) >= maxMultiple)
  138.         continue;
  139.     return (rnum % myRange);
  140. }
  141.  
  142. /*
  143.  *-----------------------------------------------------------------------------
  144.  *
  145.  * Tcl_RandomCmd  --
  146.  *     Implements the TCL random command:
  147.  *     random limit | seed ?seedval?
  148.  *
  149.  * Results:
  150.  *  Standard TCL results.
  151.  *
  152.  *-----------------------------------------------------------------------------
  153.  */
  154. int
  155. Tcl_RandomCmd (clientData, interp, argc, argv)
  156.     ClientData  clientData;
  157.     Tcl_Interp *interp;
  158.     int         argc;
  159.     char      **argv;
  160. {
  161.     long range;
  162.  
  163.     if ((argc < 2) || (argc > 3))
  164.         goto invalidArgs;
  165.  
  166.     if (STREQU (argv [1], "seed")) {
  167.         unsigned seed;
  168.  
  169.         if (argc == 3) {
  170.             if (Tcl_GetUnsigned (interp, argv[2], &seed) != TCL_OK)
  171.                 return TCL_ERROR;
  172.         } else
  173.             seed = (unsigned) (getpid() + time((time_t *)NULL));
  174.  
  175.         srandom (seed);
  176.  
  177.     } else {
  178.         if (argc != 2)
  179.             goto invalidArgs;
  180.         if (Tcl_GetLong (interp, argv[1], &range) != TCL_OK)
  181.             return TCL_ERROR;
  182.         if ((range <= 0) || (range > RANDOM_RANGE))
  183.             goto outOfRange;
  184.  
  185.         sprintf (interp->result, "%ld", ReallyRandom (range));
  186.     }
  187.     return TCL_OK;
  188.  
  189. invalidArgs:
  190.     Tcl_AppendResult (interp, tclXWrongArgs, argv [0], 
  191.                       " limit | seed ?seedval?", (char *) NULL);
  192.     return TCL_ERROR;
  193. outOfRange:
  194.     {
  195.         char buf [18];
  196.  
  197.         sprintf (buf, "%ld", RANDOM_RANGE);
  198.         Tcl_AppendResult (interp, "range must be > 0 and <= ",
  199.                           buf, (char *) NULL);
  200.         return TCL_ERROR;
  201.     }
  202. }
  203.